package com.example.exam.controller;

import com.example.exam.entity.Question;
import com.example.exam.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (Question)表控制层
 *
 * @author makejava
 * @since 2021-09-04 15:02:45
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    /**
     * 服务对象
     */
    @Autowired
    private QuestionService questionService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne/{id}")
    public Question selectOne(@PathVariable("id") Integer id) {

        return this.questionService.queryById(id);
    }

    @GetMapping("selectAll")
    public List<Question> selectAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        return this.questionService.queryAllByLimit(page, limit);
    }

    /**
     * 插入一张新的试卷
     *
     * @param question
     * @return
     */
    @RequestMapping("/insertOne")
    public Question insertOne(Question question, HttpSession session) {
        Question q = this.questionService.insert(question);
        q.setPaperid(question.getQuestionid());
        session.setAttribute("questionId", question.getQuestionid());
        session.setAttribute("flag", question.getQuestionid());
        question.setPaperid(question.getQuestionid());
        return this.questionService.update(q);
    }

    @RequestMapping("/delete")
    public boolean deleteOne(Integer questionId) {
        boolean b = questionService.deleteById(questionId);
        return b;
    }
}